<template>
  <div>
    <el-space direction="vertical" :size="20">
      <h1>用户管理</h1>
      <el-table :data="users" style="width: 100%">
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="join_date" label="加入时间" />
        <el-table-column label="员工状态操作">
          <template #default="scope">
            <el-tag v-if="scope.row.is_staff">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="用户状态操作">
          <template #default="scope">
            <el-tag v-if="scope.row.is_active" type="success">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="danger" circle size="mini" @click="onActiveUserClick(scope.$index)">
              <el-icon>
                <delete />
              </el-icon>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align: center;">
        <el-pagination background layout="prev, pager, next" :total="total_count" :current-page="page"
          @current-change="onPageChanged">
        </el-pagination>
      </div>
    </el-space>

    <!-- 删除确认对话框 -->
    <el-dialog v-model="confirmDialogVisible" title="提示" width="30%">
      <span>{{ message }}</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="confirmDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="onConfirmActiveUserClick">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { Delete } from "@element-plus/icons";
import { ElMessage } from 'element-plus';
export default {
  name: "UserPage",
  data() {
    return {
      confirmDialogVisible: false,
      users: [],
      activingIndex: 0,
      message: "",
      total_count: 0,
      page: 1
    }
  },
  mounted() {
    this.getUserList(1);
  },
  methods: {
    //获取用户列表
    getUserList(page) {
      this.$http.getuserlist(page).then(res => {
        let data = res['data']
        this.users = data['user_list']
        this.page = data['page']
        this.total_count = data['total_count']
      });
    },
    //用户状态操作（激活和禁用）
    onActiveUserClick(index) {
      this.activingIndex = index;
      this.confirmDialogVisible = true;
      const user = this.users[index];
      if (user.is_active) {
        this.message = "您确定要拉黑此用户吗？"
      } else {
        this.message = "您确定要取消拉黑此用户吗？"
      }
    },
    onConfirmActiveUserClick() {
      let user = this.users[this.activingIndex];
      let is_active = user.is_active ? 0 : 1;
      this.$http.setuserstatus(user.id, is_active).then(res => {
        if (res && res['code'] == 200) {
          ElMessage.success("操作成功！");
          this.confirmDialogVisible = false;
          let user = res.data;
          this.users.splice(this.activingIndex, 1, user);
        } else {
          ElMessage.info("操作失败！");
          console.log(res)
          this.confirmDialogVisible = false;
        }
      })
    },
    onPageChanged(page) {
      this.getUserList(page)
    }
  },
  components: {
    Delete
  }
}
</script>

<style scoped>
.el-space {
  display: block;
}
</style>